package com.turkcell.dv.dao;

import java.util.List;

import org.hibernate.SessionFactory;

import com.turkcell.dv.model.BusinessProcessDOM;

import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional(readOnly = true)
public class BusinessProcessDAOImpl implements BusinessProcessDAO{

	private static final Logger logger = LoggerFactory.getLogger(BusinessProcessDAOImpl.class);
	 
	@Autowired private SessionFactory sessionFactory;
    
    public void setSessionFactory(SessionFactory sf){
        this.sessionFactory = sf;
    }
    
	@Override
	public void addBusinessProcess(BusinessProcessDOM bp) {
		Session session = this.sessionFactory.getCurrentSession();
        session.persist(bp);
        logger.info("BusinessProcess saved successfully, BusinessProcess Details= " + bp);
    }

	@Override
	public void updateBusinessProcess(BusinessProcessDOM bp) {
		Session session = this.sessionFactory.getCurrentSession();
        session.update(bp);
        logger.info("BusinessProcess updated successfully, BusinessProcess Details=" + bp);
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<BusinessProcessDOM> listBusinessProcesss() {
        Session session = this.sessionFactory.getCurrentSession();
        List<BusinessProcessDOM> BusinessProcesssList = session.createQuery("from BusinessProcessDOM").list();
        for(BusinessProcessDOM bp : BusinessProcesssList){
            logger.info("BusinessProcess List::" + bp);
        }
        return BusinessProcesssList;
	}

	@Override
	public BusinessProcessDOM getBusinessProcessById(long id) {
	      Session session = this.sessionFactory.getCurrentSession();      
	        BusinessProcessDOM bp = (BusinessProcessDOM) session.load(BusinessProcessDOM.class, new Long(id));
	        logger.info("BusinessProcess loaded successfully, BusinessProcess details=" + bp);
	        return bp;
	}

	@Override
	public void removeBusinessProcess(long id) {
        Session session = this.sessionFactory.getCurrentSession();
        BusinessProcessDOM bp = (BusinessProcessDOM) session.load(BusinessProcessDOM.class, new Long(id));
        if(null != bp){
            session.delete(bp);
        }
        logger.info("BusinessProcess deleted successfully, BusinessProcess details=" + bp);
	}

}
